package joney.main.views.table;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.Vector;

import joney.model.Account;
import joney.model.Expense;

/**
 * Data Source that links DataModel->expenseList->table
 * @author Sean McGrail
 *
 */
public class ExpenseList {

	private Set<IExpenseListViewer> changeListeners = new HashSet();
	private Account account;
	
	public ExpenseList(Account a)
	{
		this.account = a;
	}
	
	
	public Vector<Expense> getExpenses()
	{
		return account.getExpenses();
	}
	 
	public Account getAccount()
	{
		return account;
	}
	
	/**
	 * Add a new task to the collection of tasks
	 */
	public void addExpense(Expense e) {

		//ADD to model
		account.addExpense(e);
		
		Iterator<IExpenseListViewer> iterator = changeListeners.iterator();
		while (iterator.hasNext())
			((IExpenseListViewer) iterator.next()).addExpense(e);
	}

	/**
	 * @param task
	 */
	public void removeExpense(Expense e) {
		
		
		//REMOVe from model
		account.removeExpense(e);
		
		Iterator<IExpenseListViewer> iterator = changeListeners.iterator();
		while (iterator.hasNext())
			((IExpenseListViewer) iterator.next()).removeExpense(e);
	}

	/**
	 * @param task
	 */
	public void expenseChanged(Expense e) {
		
		//UPDATE in model
		
		
		Iterator<IExpenseListViewer> iterator = changeListeners.iterator();
		while (iterator.hasNext())
			((IExpenseListViewer) iterator.next()).updateExpense(e);
	}
	
	
	/**
	 * @param viewer
	 */
	public void removeChangeListener(IExpenseListViewer viewer) {
		changeListeners.remove(viewer);
	}

	/**
	 * @param viewer
	 */
	public void addChangeListener(IExpenseListViewer viewer) {
		changeListeners.add(viewer);
	}
}
